<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>SIMD | 一码编程</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/img/favicon.ico">
    <script data-ad-client="ca-pub-7828333725993554" async="async" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <meta name="description" content="web前端技术博客,简洁至上,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github等技术文章。">
    <meta name="keywords" content="Java 知识体系,个人技术博客,后端,后端开发,后端框架,web前端,后端面试题,技术文档,学习,面试,Spring,Java,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.8c0de3bf.css" as="style"><link rel="preload" href="/assets/js/app.66ec18f0.js" as="script"><link rel="preload" href="/assets/js/2.94569d7c.js" as="script"><link rel="preload" href="/assets/js/132.7c898663.js" as="script"><link rel="prefetch" href="/assets/js/10.9f6df3f3.js"><link rel="prefetch" href="/assets/js/100.90b5eafa.js"><link rel="prefetch" href="/assets/js/101.e7cf2503.js"><link rel="prefetch" href="/assets/js/102.132ee4da.js"><link rel="prefetch" href="/assets/js/103.c0457c15.js"><link rel="prefetch" href="/assets/js/104.7355256c.js"><link rel="prefetch" href="/assets/js/105.90c948fd.js"><link rel="prefetch" href="/assets/js/106.4ce0d125.js"><link rel="prefetch" href="/assets/js/107.b18bd269.js"><link rel="prefetch" href="/assets/js/108.3bed0655.js"><link rel="prefetch" href="/assets/js/109.40f361d8.js"><link rel="prefetch" href="/assets/js/11.a83bdce3.js"><link rel="prefetch" href="/assets/js/110.d452c033.js"><link rel="prefetch" href="/assets/js/111.fc0320ab.js"><link rel="prefetch" href="/assets/js/112.f149f0fc.js"><link rel="prefetch" href="/assets/js/113.8f2e4a42.js"><link rel="prefetch" href="/assets/js/114.14bae374.js"><link rel="prefetch" href="/assets/js/115.9cdc00eb.js"><link rel="prefetch" href="/assets/js/116.e473e37b.js"><link rel="prefetch" href="/assets/js/117.8e77ea25.js"><link rel="prefetch" href="/assets/js/118.3db4ad27.js"><link rel="prefetch" href="/assets/js/119.cbf28d7b.js"><link rel="prefetch" href="/assets/js/12.95a37db8.js"><link rel="prefetch" href="/assets/js/120.94bba4c6.js"><link rel="prefetch" href="/assets/js/121.5515ee13.js"><link rel="prefetch" href="/assets/js/122.fc2a0ae7.js"><link rel="prefetch" href="/assets/js/123.de0f3682.js"><link rel="prefetch" href="/assets/js/124.d835e05f.js"><link rel="prefetch" href="/assets/js/125.8c9d9f85.js"><link rel="prefetch" href="/assets/js/126.01ef7d9d.js"><link rel="prefetch" href="/assets/js/127.61e602a5.js"><link rel="prefetch" href="/assets/js/128.c7d8277c.js"><link rel="prefetch" href="/assets/js/129.245b12e4.js"><link rel="prefetch" href="/assets/js/13.1acbcf03.js"><link rel="prefetch" href="/assets/js/130.56a62f57.js"><link rel="prefetch" href="/assets/js/131.dabeff40.js"><link rel="prefetch" href="/assets/js/133.fe172050.js"><link rel="prefetch" href="/assets/js/134.ed97034b.js"><link rel="prefetch" href="/assets/js/135.b21206db.js"><link rel="prefetch" href="/assets/js/136.9c949bb5.js"><link rel="prefetch" href="/assets/js/137.ca167f36.js"><link rel="prefetch" href="/assets/js/138.78397eaf.js"><link rel="prefetch" href="/assets/js/139.4bb796c1.js"><link rel="prefetch" href="/assets/js/14.d4f8ea8f.js"><link rel="prefetch" href="/assets/js/140.e1a6fba1.js"><link rel="prefetch" href="/assets/js/141.c301b64c.js"><link rel="prefetch" href="/assets/js/142.0c707d5f.js"><link rel="prefetch" href="/assets/js/143.1ec2981e.js"><link rel="prefetch" href="/assets/js/144.a57572b6.js"><link rel="prefetch" href="/assets/js/145.350cbe8a.js"><link rel="prefetch" href="/assets/js/146.c050e9e0.js"><link rel="prefetch" href="/assets/js/147.10ee482a.js"><link rel="prefetch" href="/assets/js/148.0fc834f1.js"><link rel="prefetch" href="/assets/js/149.77dd57bf.js"><link rel="prefetch" href="/assets/js/15.ce7515db.js"><link rel="prefetch" href="/assets/js/150.4fc76d42.js"><link rel="prefetch" href="/assets/js/151.73c875ec.js"><link rel="prefetch" href="/assets/js/152.e0b6c00d.js"><link rel="prefetch" href="/assets/js/153.6c120d67.js"><link rel="prefetch" href="/assets/js/154.01051fe1.js"><link rel="prefetch" href="/assets/js/155.2a17fb22.js"><link rel="prefetch" href="/assets/js/156.ada61aaa.js"><link rel="prefetch" href="/assets/js/157.248dc039.js"><link rel="prefetch" href="/assets/js/158.0d5f94bd.js"><link rel="prefetch" href="/assets/js/159.9c3deeda.js"><link rel="prefetch" href="/assets/js/16.4b68c1d3.js"><link rel="prefetch" href="/assets/js/160.d95c292d.js"><link rel="prefetch" href="/assets/js/161.56f971fa.js"><link rel="prefetch" href="/assets/js/162.232ce423.js"><link rel="prefetch" href="/assets/js/163.92323a72.js"><link rel="prefetch" href="/assets/js/164.275aa708.js"><link rel="prefetch" href="/assets/js/165.d47cde30.js"><link rel="prefetch" href="/assets/js/166.c8299909.js"><link rel="prefetch" href="/assets/js/167.974fe7cf.js"><link rel="prefetch" href="/assets/js/168.18a8c64f.js"><link rel="prefetch" href="/assets/js/169.b2adb3d2.js"><link rel="prefetch" href="/assets/js/17.e586f917.js"><link rel="prefetch" href="/assets/js/170.9d53a901.js"><link rel="prefetch" href="/assets/js/171.5a65edf7.js"><link rel="prefetch" href="/assets/js/172.44a7f551.js"><link rel="prefetch" href="/assets/js/173.19ca9e62.js"><link rel="prefetch" href="/assets/js/174.7d4d7be5.js"><link rel="prefetch" href="/assets/js/175.6aa7939a.js"><link rel="prefetch" href="/assets/js/176.b82e1645.js"><link rel="prefetch" href="/assets/js/177.8c9727b0.js"><link rel="prefetch" href="/assets/js/178.6c4fa6f0.js"><link rel="prefetch" href="/assets/js/179.91817012.js"><link rel="prefetch" href="/assets/js/18.f8479181.js"><link rel="prefetch" href="/assets/js/180.bdded9da.js"><link rel="prefetch" href="/assets/js/181.39ee6cdc.js"><link rel="prefetch" href="/assets/js/182.7a736049.js"><link rel="prefetch" href="/assets/js/183.b3f14f77.js"><link rel="prefetch" href="/assets/js/184.739caa46.js"><link rel="prefetch" href="/assets/js/185.f4abf18e.js"><link rel="prefetch" href="/assets/js/186.91a57d34.js"><link rel="prefetch" href="/assets/js/187.6694e5f5.js"><link rel="prefetch" href="/assets/js/188.b4bd2a24.js"><link rel="prefetch" href="/assets/js/189.488eaf65.js"><link rel="prefetch" href="/assets/js/19.270533e4.js"><link rel="prefetch" href="/assets/js/190.2f3c9e73.js"><link rel="prefetch" href="/assets/js/191.c8bff233.js"><link rel="prefetch" href="/assets/js/192.4f05c3b5.js"><link rel="prefetch" href="/assets/js/193.24b7b232.js"><link rel="prefetch" href="/assets/js/194.89ab4683.js"><link rel="prefetch" href="/assets/js/195.49ed7290.js"><link rel="prefetch" href="/assets/js/196.025c381d.js"><link rel="prefetch" href="/assets/js/197.c4f8a6b1.js"><link rel="prefetch" href="/assets/js/198.cc687156.js"><link rel="prefetch" href="/assets/js/199.c842f262.js"><link rel="prefetch" href="/assets/js/20.6f9d7815.js"><link rel="prefetch" href="/assets/js/200.15fb0a60.js"><link rel="prefetch" href="/assets/js/201.b3f4f118.js"><link rel="prefetch" href="/assets/js/202.cedfcb88.js"><link rel="prefetch" href="/assets/js/203.f860c15b.js"><link rel="prefetch" href="/assets/js/204.2d6ce65d.js"><link rel="prefetch" href="/assets/js/205.7c78e462.js"><link rel="prefetch" href="/assets/js/206.cd2f039c.js"><link rel="prefetch" href="/assets/js/207.cccbfddd.js"><link rel="prefetch" href="/assets/js/208.21e75d6f.js"><link rel="prefetch" href="/assets/js/209.65effd68.js"><link rel="prefetch" href="/assets/js/21.1ad2d3c1.js"><link rel="prefetch" href="/assets/js/210.9e71a7f5.js"><link rel="prefetch" href="/assets/js/211.969e72ad.js"><link rel="prefetch" href="/assets/js/212.820f8094.js"><link rel="prefetch" href="/assets/js/213.df5ac78b.js"><link rel="prefetch" href="/assets/js/214.d9b77af5.js"><link rel="prefetch" href="/assets/js/215.08b2a419.js"><link rel="prefetch" href="/assets/js/216.38df1922.js"><link rel="prefetch" href="/assets/js/217.d3933fe8.js"><link rel="prefetch" href="/assets/js/218.eb3fecfe.js"><link rel="prefetch" href="/assets/js/219.7c5c54ad.js"><link rel="prefetch" href="/assets/js/22.8628d841.js"><link rel="prefetch" href="/assets/js/220.263411db.js"><link rel="prefetch" href="/assets/js/221.ea2bc4d9.js"><link rel="prefetch" href="/assets/js/222.e3fc7753.js"><link rel="prefetch" href="/assets/js/223.2658ac0c.js"><link rel="prefetch" href="/assets/js/224.ad59b2ea.js"><link rel="prefetch" href="/assets/js/225.639296c3.js"><link rel="prefetch" href="/assets/js/226.ee8abb8a.js"><link rel="prefetch" href="/assets/js/227.70ba5a31.js"><link rel="prefetch" href="/assets/js/228.217a5ee3.js"><link rel="prefetch" href="/assets/js/229.236c5847.js"><link rel="prefetch" href="/assets/js/23.6f676329.js"><link rel="prefetch" href="/assets/js/230.989f1030.js"><link rel="prefetch" href="/assets/js/231.93917c5d.js"><link rel="prefetch" href="/assets/js/232.84c835d7.js"><link rel="prefetch" href="/assets/js/233.c84a49e1.js"><link rel="prefetch" href="/assets/js/234.d99384c2.js"><link rel="prefetch" href="/assets/js/235.ab6a57aa.js"><link rel="prefetch" href="/assets/js/236.a2c423a6.js"><link rel="prefetch" href="/assets/js/237.e1b8b45c.js"><link rel="prefetch" href="/assets/js/238.ebc7a473.js"><link rel="prefetch" href="/assets/js/239.a13c8156.js"><link rel="prefetch" href="/assets/js/24.c8e5b352.js"><link rel="prefetch" href="/assets/js/240.701e1d60.js"><link rel="prefetch" href="/assets/js/241.fc7fdaad.js"><link rel="prefetch" href="/assets/js/242.50a84697.js"><link rel="prefetch" href="/assets/js/243.3a65c46e.js"><link rel="prefetch" href="/assets/js/244.c2d3da27.js"><link rel="prefetch" href="/assets/js/245.90cf114d.js"><link rel="prefetch" href="/assets/js/246.175e42a8.js"><link rel="prefetch" href="/assets/js/247.c96948b7.js"><link rel="prefetch" href="/assets/js/248.e7c91eb4.js"><link rel="prefetch" href="/assets/js/249.f54451d6.js"><link rel="prefetch" href="/assets/js/25.714b7be8.js"><link rel="prefetch" href="/assets/js/250.e7583b33.js"><link rel="prefetch" href="/assets/js/251.2cd29c41.js"><link rel="prefetch" href="/assets/js/26.bd9da114.js"><link rel="prefetch" href="/assets/js/27.c219321c.js"><link rel="prefetch" href="/assets/js/28.45088c1e.js"><link rel="prefetch" href="/assets/js/29.09bf218e.js"><link rel="prefetch" href="/assets/js/3.39144ebb.js"><link rel="prefetch" href="/assets/js/30.011f667b.js"><link rel="prefetch" href="/assets/js/31.a3145b0e.js"><link rel="prefetch" href="/assets/js/32.61a565cb.js"><link rel="prefetch" href="/assets/js/33.b0e79d8f.js"><link rel="prefetch" href="/assets/js/34.6ce517b2.js"><link rel="prefetch" href="/assets/js/35.a55f3b4b.js"><link rel="prefetch" href="/assets/js/36.5cb6f5ab.js"><link rel="prefetch" href="/assets/js/37.dc818991.js"><link rel="prefetch" href="/assets/js/38.7287d147.js"><link rel="prefetch" href="/assets/js/39.d05c15ae.js"><link rel="prefetch" href="/assets/js/4.ffde19c5.js"><link rel="prefetch" href="/assets/js/40.8e92777a.js"><link rel="prefetch" href="/assets/js/41.9afe0600.js"><link rel="prefetch" href="/assets/js/42.2398ae2a.js"><link rel="prefetch" href="/assets/js/43.42eb6d55.js"><link rel="prefetch" href="/assets/js/44.11c37dde.js"><link rel="prefetch" href="/assets/js/45.ca695a69.js"><link rel="prefetch" href="/assets/js/46.4f3e0b61.js"><link rel="prefetch" href="/assets/js/47.12e82ba7.js"><link rel="prefetch" href="/assets/js/48.75dc7298.js"><link rel="prefetch" href="/assets/js/49.80a6a902.js"><link rel="prefetch" href="/assets/js/5.808dc17c.js"><link rel="prefetch" href="/assets/js/50.a4ae29db.js"><link rel="prefetch" href="/assets/js/51.d9db2fa7.js"><link rel="prefetch" href="/assets/js/52.6a0cab7a.js"><link rel="prefetch" href="/assets/js/53.d20e8f05.js"><link rel="prefetch" href="/assets/js/54.1d01c783.js"><link rel="prefetch" href="/assets/js/55.5193632e.js"><link rel="prefetch" href="/assets/js/56.b2b245c0.js"><link rel="prefetch" href="/assets/js/57.79e68e16.js"><link rel="prefetch" href="/assets/js/58.15f5a489.js"><link rel="prefetch" href="/assets/js/59.5036aeff.js"><link rel="prefetch" href="/assets/js/6.a9f6899c.js"><link rel="prefetch" href="/assets/js/60.6b2f3d1f.js"><link rel="prefetch" href="/assets/js/61.93b24a50.js"><link rel="prefetch" href="/assets/js/62.91a7cb5d.js"><link rel="prefetch" href="/assets/js/63.07900dfe.js"><link rel="prefetch" href="/assets/js/64.bf177ab5.js"><link rel="prefetch" href="/assets/js/65.3bdfad21.js"><link rel="prefetch" href="/assets/js/66.38ff0639.js"><link rel="prefetch" href="/assets/js/67.2d4ca96e.js"><link rel="prefetch" href="/assets/js/68.67315a94.js"><link rel="prefetch" href="/assets/js/69.02ceaaa8.js"><link rel="prefetch" href="/assets/js/7.d6405d52.js"><link rel="prefetch" href="/assets/js/70.6dd6d824.js"><link rel="prefetch" href="/assets/js/71.c322ae43.js"><link rel="prefetch" href="/assets/js/72.8c122a54.js"><link rel="prefetch" href="/assets/js/73.52351bd6.js"><link rel="prefetch" href="/assets/js/74.e63e7849.js"><link rel="prefetch" href="/assets/js/75.ea8d8d16.js"><link rel="prefetch" href="/assets/js/76.6a3cf968.js"><link rel="prefetch" href="/assets/js/77.da0156e4.js"><link rel="prefetch" href="/assets/js/78.1ff3f855.js"><link rel="prefetch" href="/assets/js/79.0e8351bf.js"><link rel="prefetch" href="/assets/js/8.87819edd.js"><link rel="prefetch" href="/assets/js/80.8ae2bc20.js"><link rel="prefetch" href="/assets/js/81.eff278ac.js"><link rel="prefetch" href="/assets/js/82.b6c26176.js"><link rel="prefetch" href="/assets/js/83.bc76aa23.js"><link rel="prefetch" href="/assets/js/84.12d6e44f.js"><link rel="prefetch" href="/assets/js/85.c5a8e231.js"><link rel="prefetch" href="/assets/js/86.881b4810.js"><link rel="prefetch" href="/assets/js/87.9c91aa9b.js"><link rel="prefetch" href="/assets/js/88.586a37db.js"><link rel="prefetch" href="/assets/js/89.f8a18a98.js"><link rel="prefetch" href="/assets/js/9.0d1d61a0.js"><link rel="prefetch" href="/assets/js/90.bd368869.js"><link rel="prefetch" href="/assets/js/91.c3a90640.js"><link rel="prefetch" href="/assets/js/92.9b1b660c.js"><link rel="prefetch" href="/assets/js/93.ce971fa1.js"><link rel="prefetch" href="/assets/js/94.f7beac4c.js"><link rel="prefetch" href="/assets/js/95.244f4715.js"><link rel="prefetch" href="/assets/js/96.c908a45c.js"><link rel="prefetch" href="/assets/js/97.37c6101f.js"><link rel="prefetch" href="/assets/js/98.651dc399.js"><link rel="prefetch" href="/assets/js/99.31028387.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8c0de3bf.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/EB-logo.png" alt="一码编程" class="logo"> <span class="site-name can-hide">一码编程</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Java" class="dropdown-title"><a href="/java" class="link-title">Java</a> <span class="title" style="display:none;">Java</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Java 基础</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/ae2dff/" class="nav-link">Java 基础知识点</a></li></ul></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java 集合框架</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java 多线程并发</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java IO/NIO/AIO</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">JVM 总结</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/linbingxing/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://avatars.githubusercontent.com/u/5601844?s=96&amp;v=4"> <div class="blogger-info"><h3>林半码</h3> <span>编程届的痞马</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Java" class="dropdown-title"><a href="/java" class="link-title">Java</a> <span class="title" style="display:none;">Java</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>Java 基础</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/ae2dff/" class="nav-link">Java 基础知识点</a></li></ul></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java 集合框架</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java 多线程并发</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">Java IO/NIO/AIO</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">JVM 总结</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端" class="dropdown-title"><a href="/web/" class="link-title">前端</a> <span class="title" style="display:none;">前端</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>前端文章</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>学习笔记</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">《JavaScript教程》</a></li><li class="dropdown-subitem"><a href="/note/js/" class="nav-link">《JavaScript高级程序设计》</a></li><li class="dropdown-subitem"><a href="/note/es6/" class="nav-link">《ES6 教程》</a></li><li class="dropdown-subitem"><a href="/note/vue/" class="nav-link">《Vue》</a></li><li class="dropdown-subitem"><a href="/note/react/" class="nav-link">《React》</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">《TypeScript 从零实现 axios》</a></li><li class="dropdown-subitem"><a href="/note/git/" class="nav-link">《Git》</a></li><li class="dropdown-subitem"><a href="/pages/51afd6/" class="nav-link">TypeScript</a></li><li class="dropdown-subitem"><a href="/pages/4643cd/" class="nav-link">JS设计模式总结</a></li></ul></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="页面" class="dropdown-title"><a href="/ui/" class="link-title">页面</a> <span class="title" style="display:none;">页面</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/8309a5b876fc95e3/" class="nav-link">HTML</a></li><li class="dropdown-item"><!----> <a href="/pages/0a83b083bdf257cb/" class="nav-link">CSS</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="技术" class="dropdown-title"><a href="/technology/" class="link-title">技术</a> <span class="title" style="display:none;">技术</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/9a7ee40fc232253e/" class="nav-link">技术文档</a></li><li class="dropdown-item"><!----> <a href="/pages/4c778760be26d8b3/" class="nav-link">GitHub技巧</a></li><li class="dropdown-item"><!----> <a href="/pages/117708e0af7f0bd9/" class="nav-link">Nodejs</a></li><li class="dropdown-item"><!----> <a href="/pages/41f87d890d0a02af/" class="nav-link">博客搭建</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="更多" class="dropdown-title"><a href="/more/" class="link-title">更多</a> <span class="title" style="display:none;">更多</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/pages/f2a556/" class="nav-link">学习</a></li><li class="dropdown-item"><!----> <a href="/pages/aea6571b7a8bae86/" class="nav-link">面试</a></li><li class="dropdown-item"><!----> <a href="/pages/2d615df9a36a98ed/" class="nav-link">心情杂货</a></li><li class="dropdown-item"><!----> <a href="/pages/baaa02/" class="nav-link">实用技巧</a></li><li class="dropdown-item"><!----> <a href="/friends/" class="nav-link">友情链接</a></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">关于</a></div><div class="nav-item"><a href="/pages/beb6c0bd8a66cea6/" class="nav-link">收藏</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div> <a href="https://github.com/linbingxing/vuepress-theme-vdoing" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><a href="/pages/f344d070a1031ef7/" class="sidebar-link">ECMAScript 6 简介</a></li><li><a href="/pages/c1edd70a6b7c7872/" class="sidebar-link">let 和 const 命令</a></li><li><a href="/pages/b1ab10a62f7564da/" class="sidebar-link">变量的解构赋值</a></li><li><a href="/pages/ca89eca8adeba5f4/" class="sidebar-link">字符串的扩展</a></li><li><a href="/pages/a650b4a0ebfc9350/" class="sidebar-link">字符串的新增方法</a></li><li><a href="/pages/0473261a6ab0ee8c/" class="sidebar-link">正则的扩展</a></li><li><a href="/pages/5dfea9a0f2d1a392/" class="sidebar-link">数值的扩展</a></li><li><a href="/pages/8ed309d668b20264/" class="sidebar-link">函数的扩展</a></li><li><a href="/pages/e34009d60d8bc4b2/" class="sidebar-link">数组的扩展</a></li><li><a href="/pages/b5e3e0a0ff6e9c25/" class="sidebar-link">对象的扩展</a></li><li><a href="/pages/e85e68947502cf90/" class="sidebar-link">对象的新增方法</a></li><li><a href="/pages/02c86eb2792f3262/" class="sidebar-link">Symbol</a></li><li><a href="/pages/0c21dae358fca16b/" class="sidebar-link">Set 和 Map 数据结构</a></li><li><a href="/pages/f56ec2ab97d60483/" class="sidebar-link">Proxy</a></li><li><a href="/pages/74de3e45e4491e95/" class="sidebar-link">Reflect</a></li><li><a href="/pages/2810ae8985e9bd52/" class="sidebar-link">Promise 对象</a></li><li><a href="/pages/48df907ad3570f3d/" class="sidebar-link">Iterator 和 for-of 循环</a></li><li><a href="/pages/718b48ed9ce0adce/" class="sidebar-link">Generator 函数的语法</a></li><li><a href="/pages/75af7031eb66847b/" class="sidebar-link">Generator 函数的异步应用</a></li><li><a href="/pages/3777253e65bac487/" class="sidebar-link">async 函数</a></li><li><a href="/pages/e831e1593c82bbe0/" class="sidebar-link">Class 的基本语法</a></li><li><a href="/pages/83f8c3a0cd87dd83/" class="sidebar-link">Class 的继承</a></li><li><a href="/pages/efe2fb04eb8ac5fb/" class="sidebar-link">Module 的语法</a></li><li><a href="/pages/a79ca2e64ceae213/" class="sidebar-link">Module 的加载实现</a></li><li><a href="/pages/984bf549204bb266/" class="sidebar-link">编程风格</a></li><li><a href="/pages/32c35f7651d6e58e/" class="sidebar-link">读懂 ECMAScript 规格</a></li><li><a href="/pages/16121351be68691b/" class="sidebar-link">异步遍历器</a></li><li><a href="/pages/a2ba314746bfdbdd/" class="sidebar-link">ArrayBuffer</a></li><li><a href="/pages/7188882b8d65af1b/" class="sidebar-link">最新提案</a></li><li><a href="/pages/e97bc1e5626b082c/" class="sidebar-link">装饰器</a></li><li><a href="/pages/1cf50330655efc69/" class="sidebar-link">函数式编程</a></li><li><a href="/pages/6a8e2dc558da1b39/" class="sidebar-link">Mixin</a></li><li><a href="/pages/8e8f80f69b775a56/" aria-current="page" class="active sidebar-link">SIMD</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#概述" class="sidebar-link">概述</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#数据类型" class="sidebar-link">数据类型</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#静态方法-数学运算" class="sidebar-link">静态方法：数学运算</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-abs-simd-type-neg" class="sidebar-link">SIMD.%type%.abs()，SIMD.%type%.neg()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-add-simd-type-addsaturate" class="sidebar-link">SIMD.%type%.add()，SIMD.%type%.addSaturate()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-sub-simd-type-subsaturate" class="sidebar-link">SIMD.%type%.sub()，SIMD.%type%.subSaturate()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-mul-simd-type-div-simd-type-sqrt" class="sidebar-link">SIMD.%type%.mul()，SIMD.%type%.div()，SIMD.%type%.sqrt()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-floattype-reciprocalapproximation-simd-type-reciprocalsqrtapproximation" class="sidebar-link">SIMD.%FloatType%.reciprocalApproximation()，SIMD.%type%.reciprocalSqrtApproximation()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-integertype-shiftleftbyscalar" class="sidebar-link">SIMD.%IntegerType%.shiftLeftByScalar()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-integertype-shiftrightbyscalar" class="sidebar-link">SIMD.%IntegerType%.shiftRightByScalar()</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#静态方法-通道处理" class="sidebar-link">静态方法：通道处理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-check" class="sidebar-link">SIMD.%type%.check()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-extractlane-simd-type-replacelane" class="sidebar-link">SIMD.%type%.extractLane()，SIMD.%type%.replaceLane()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-load" class="sidebar-link">SIMD.%type%.load()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-store" class="sidebar-link">SIMD.%type%.store()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-splat" class="sidebar-link">SIMD.%type%.splat()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-swizzle" class="sidebar-link">SIMD.%type%.swizzle()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-shuffle" class="sidebar-link">SIMD.%type%.shuffle()</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#静态方法-比较运算" class="sidebar-link">静态方法：比较运算</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-equal-simd-type-notequal" class="sidebar-link">SIMD.%type%.equal()，SIMD.%type%.notEqual()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-greaterthan-simd-type-greaterthanorequal" class="sidebar-link">SIMD.%type%.greaterThan()，SIMD.%type%.greaterThanOrEqual()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-lessthan-simd-type-lessthanorequal" class="sidebar-link">SIMD.%type%.lessThan()，SIMD.%type%.lessThanOrEqual()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-select" class="sidebar-link">SIMD.%type%.select()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-booleantype-alltrue-simd-booleantype-anytrue" class="sidebar-link">SIMD.%BooleanType%.allTrue()，SIMD.%BooleanType%.anyTrue()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-min-simd-type-minnum" class="sidebar-link">SIMD.%type%.min()，SIMD.%type%.minNum()</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-max-simd-type-maxnum" class="sidebar-link">SIMD.%type%.max()，SIMD.%type%.maxNum()</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#静态方法-位运算" class="sidebar-link">静态方法：位运算</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-and-simd-type-or-simd-type-xor-simd-type-not" class="sidebar-link">SIMD.%type%.and()，SIMD.%type%.or()，SIMD.%type%.xor()，SIMD.%type%.not()</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#静态方法-数据类型转换" class="sidebar-link">静态方法：数据类型转换</a></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#实例方法" class="sidebar-link">实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#simd-type-prototype-tostring" class="sidebar-link">SIMD.%type%.prototype.toString()</a></li></ul></li><li class="sidebar-sub-header"><a href="/pages/8e8f80f69b775a56/#实例-求平均值" class="sidebar-link">实例：求平均值</a></li></ul></li><li><a href="/pages/ea6f3b870f6dab69/" class="sidebar-link">参考链接</a></li></ul> <div class="sidebar-slot sidebar-slot-bottom"><!-- 正方形 -->
      <ins class="adsbygoogle"
          style="display:block"
          data-ad-client="ca-pub-7828333725993554"
          data-ad-slot="3508773082"
          data-ad-format="auto"
          data-full-width-responsive="true"></ins>
      <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
      </script></div></aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-1cd794fe><div class="articleInfo" data-v-1cd794fe><ul class="breadcrumbs" data-v-1cd794fe><li data-v-1cd794fe><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-1cd794fe></a></li> <li data-v-1cd794fe><a href="/note/es6/" title="《ES6 教程》笔记-目录页" data-v-1cd794fe>《ES6 教程》笔记</a></li> <!----> <!----></ul> <div class="info" data-v-1cd794fe><div title="作者" class="author iconfont icon-touxiang" data-v-1cd794fe><a href="javascript:;" data-v-1cd794fe>阮一峰</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-1cd794fe><a href="javascript:;" data-v-1cd794fe>2020-02-09</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-content"></div></div></div> <h1><img src="">
          SIMD
        </h1> <div class="page-slot page-slot-top"><!-- 固定100% * 90px可显示，max-height:90px未见显示-->
     <ins class="adsbygoogle"
          style="display:inline-block;width:100%;max-height:90px"
          data-ad-client="ca-pub-7828333725993554"
          data-ad-slot="6625304284"></ins>
      <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
      </script></div> <div class="theme-vdoing-content content__default"><h1 id="simd"><a href="#simd" class="header-anchor">#</a> SIMD</h1> <h2 id="概述"><a href="#概述" class="header-anchor">#</a> 概述</h2> <p>SIMD（发音<code>/sim-dee/</code>）是“Single Instruction/Multiple Data”的缩写，意为“单指令，多数据”。它是 JavaScript 操作 CPU 对应指令的接口，你可以看做这是一种不同的运算执行模式。与它相对的是 SISD（“Single Instruction/Single Data”），即“单指令，单数据”。

SIMD 的含义是使用一个指令，完成多个数据的运算；SISD 的含义是使用一个指令，完成单个数据的运算，这是 JavaScript 的默认运算模式。显而易见，SIMD 的执行效率要高于 SISD，所以被广泛用于 3D 图形运算、物理模拟等运算量超大的项目之中。</p> <p>为了理解 SIMD，请看下面的例子。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

c<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">+</span> b<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
c<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> b<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
c<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">+</span> b<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
c<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">+</span> b<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
c <span class="token comment">// Array[6, 8, 10, 12]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中，数组<code>a</code>和<code>b</code>的对应成员相加，结果放入数组<code>c</code>。它的运算模式是依次处理每个数组成员，一共有四个数组成员，所以需要运算 4 次。</p> <p>如果采用 SIMD 模式，只要运算一次就够了。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Float32x4[6, 8, 10, 12]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码之中，数组<code>a</code>和<code>b</code>的四个成员的各自相加，只用一条指令就完成了。因此，速度比上一种写法提高了 4 倍。</p> <p>一次 SIMD 运算，可以处理多个数据，这些数据被称为“通道”（lane）。上面代码中，一次运算了四个数据，因此就是四个通道。</p> <p>SIMD 通常用于矢量运算。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code>v <span class="token operator">+</span> w <span class="token operator">=</span> 〈v1<span class="token punctuation">,</span> …<span class="token punctuation">,</span> vn〉<span class="token operator">+</span> 〈w1<span class="token punctuation">,</span> …<span class="token punctuation">,</span> wn〉
      <span class="token operator">=</span> 〈v1<span class="token operator">+</span>w1<span class="token punctuation">,</span> …<span class="token punctuation">,</span> vn<span class="token operator">+</span>wn〉
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面代码中，<code>v</code>和<code>w</code>是两个多元矢量。它们的加运算，在 SIMD 下是一个指令、而不是 n 个指令完成的，这就大大提高了效率。这对于 3D 动画、图像处理、信号处理、数值处理、加密等运算是非常重要的。比如，Canvas 的<code>getImageData()</code>会将图像文件读成一个二进制数组，SIMD 就很适合对于这种数组的处理。</p> <p>总的来说，SIMD 是数据并行处理（parallelism）的一种手段，可以加速一些运算密集型操作的速度。将来与 WebAssembly 结合以后，可以让 JavaScript 达到二进制代码的运行速度。</p> <h2 id="数据类型"><a href="#数据类型" class="header-anchor">#</a> 数据类型</h2> <p>SIMD 提供 12 种数据类型，总长度都是 128 个二进制位。</p> <ul><li>Float32x4：四个 32 位浮点数</li> <li>Float64x2：两个 64 位浮点数</li> <li>Int32x4：四个 32 位整数</li> <li>Int16x8：八个 16 位整数</li> <li>Int8x16：十六个 8 位整数</li> <li>Uint32x4：四个无符号的 32 位整数</li> <li>Uint16x8：八个无符号的 16 位整数</li> <li>Uint8x16：十六个无符号的 8 位整数</li> <li>Bool32x4：四个 32 位布尔值</li> <li>Bool16x8：八个 16 位布尔值</li> <li>Bool8x16：十六个 8 位布尔值</li> <li>Bool64x2：两个 64 位布尔值</li></ul> <p>每种数据类型被<code>x</code>符号分隔成两部分，后面的部分表示通道数，前面的部分表示每个通道的宽度和类型。比如，<code>Float32x4</code>就表示这个值有 4 个通道，每个通道是一个 32 位浮点数。</p> <p>每个通道之中，可以放置四种数据。</p> <ul><li>浮点数（float，比如 1.0）</li> <li>带符号的整数（Int，比如-1）</li> <li>无符号的整数（Uint，比如 1）</li> <li>布尔值（Bool，包含<code>true</code>和<code>false</code>两种值）</li></ul> <p>每种 SIMD 的数据类型都是一个函数方法，可以传入参数，生成对应的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>上面代码中，变量<code>a</code>就是一个 128 位、包含四个 32 位浮点数（即四个通道）的值。</p> <p>注意，这些数据类型方法都不是构造函数，前面不能加<code>new</code>，否则会报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> v <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SIMD<span class="token punctuation">.</span>Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// TypeError: SIMD.Float32x4 is not a constructor</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="静态方法-数学运算"><a href="#静态方法-数学运算" class="header-anchor">#</a> 静态方法：数学运算</h2> <p>每种数据类型都有一系列运算符，支持基本的数学运算。</p> <h3 id="simd-type-abs-simd-type-neg"><a href="#simd-type-abs-simd-type-neg" class="header-anchor">#</a> SIMD.%type%.abs()，SIMD.%type%.neg()</h3> <p><code>abs</code>方法接受一个 SIMD 值作为参数，将它的每个通道都转成绝对值，作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">// Float32x4[1, 2, 0, NaN]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>neg</code>方法接受一个 SIMD 值作为参数，将它的每个通道都转成负值，作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">neg</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">// Float32x4[1, 2, -3, -0]</span>

<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">Infinity</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">neg</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
<span class="token comment">// Float64x2[NaN, -Infinity]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="simd-type-add-simd-type-addsaturate"><a href="#simd-type-add-simd-type-addsaturate" class="header-anchor">#</a> SIMD.%type%.add()，SIMD.%type%.addSaturate()</h3> <p><code>add</code>方法接受两个 SIMD 值作为参数，将它们的每个通道相加，作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">5.0</span><span class="token punctuation">,</span> <span class="token number">10.0</span><span class="token punctuation">,</span> <span class="token number">15.0</span><span class="token punctuation">,</span> <span class="token number">20.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中，经过加法运算，新的 SIMD 值为<code>(6.0, 12.0, 18.0. 24.0)</code>。</p> <p><code>addSaturate</code>方法跟<code>add</code>方法的作用相同，都是两个通道相加，但是溢出的处理不一致。对于<code>add</code>方法，如果两个值相加发生溢出，溢出的二进制位会被丢弃; <code>addSaturate</code>方法则是返回该数据类型的最大值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint16x8</span><span class="token punctuation">(</span><span class="token number">65533</span><span class="token punctuation">,</span> <span class="token number">65534</span><span class="token punctuation">,</span> <span class="token number">65535</span><span class="token punctuation">,</span> <span class="token number">65535</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint16x8</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Uint16x8<span class="token punctuation">.</span><span class="token function">addSaturate</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Uint16x8[65534, 65535, 65535, 65535, 2, 2, 2, 2]</span>

<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int16x8</span><span class="token punctuation">(</span><span class="token number">32765</span><span class="token punctuation">,</span> <span class="token number">32766</span><span class="token punctuation">,</span> <span class="token number">32767</span><span class="token punctuation">,</span> <span class="token number">32767</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int16x8</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int16x8<span class="token punctuation">.</span><span class="token function">addSaturate</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int16x8[32766, 32767, 32767, 32767, 2, 2, 2, 2]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中，<code>Uint16</code>的最大值是 65535，<code>Int16</code>的最大值是 32767。一旦发生溢出，就返回这两个值。</p> <p>注意，<code>Uint32x4</code>和<code>Int32x4</code>这两种数据类型没有<code>addSaturate</code>方法。</p> <h3 id="simd-type-sub-simd-type-subsaturate"><a href="#simd-type-sub-simd-type-subsaturate" class="header-anchor">#</a> SIMD.%type%.sub()，SIMD.%type%.subSaturate()</h3> <p><code>sub</code>方法接受两个 SIMD 值作为参数，将它们的每个通道相减，作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">sub</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Float32x4[-4, -5, 0, 1]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>subSaturate</code>方法跟<code>sub</code>方法的作用相同，都是两个通道相减，但是溢出的处理不一致。对于<code>sub</code>方法，如果两个值相减发生溢出，溢出的二进制位会被丢弃; <code>subSaturate</code>方法则是返回该数据类型的最小值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint16x8</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint16x8</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Uint16x8<span class="token punctuation">.</span><span class="token function">subSaturate</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Uint16x8[0, 0, 0, 0, 0, 0, 0, 0]</span>

<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int16x8</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int16x8</span><span class="token punctuation">(</span><span class="token number">32767</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int16x8<span class="token punctuation">.</span><span class="token function">subSaturate</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span>
<span class="token comment">// Int16x8[-32768, 0, 0, 0, 0, 0, 0, 0, 0]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中，<code>Uint16</code>的最小值是<code>0</code>，<code>Int16</code>的最小值是<code>-32678</code>。一旦运算发生溢出，就返回最小值。</p> <h3 id="simd-type-mul-simd-type-div-simd-type-sqrt"><a href="#simd-type-mul-simd-type-div-simd-type-sqrt" class="header-anchor">#</a> SIMD.%type%.mul()，SIMD.%type%.div()，SIMD.%type%.sqrt()</h3> <p><code>mul</code>方法接受两个 SIMD 值作为参数，将它们的每个通道相乘，作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">mul</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Float32x4[-3, -6, 9, 12]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>div</code>方法接受两个 SIMD 值作为参数，将它们的每个通道相除，作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">div</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Float32x4[0.5, 0.5, 0.5, 0.5]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>sqrt</code>方法接受一个 SIMD 值作为参数，求出每个通道的平方根，作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span>
<span class="token comment">// Float64x2[2, 2.8284271247461903]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="simd-floattype-reciprocalapproximation-simd-type-reciprocalsqrtapproximation"><a href="#simd-floattype-reciprocalapproximation-simd-type-reciprocalsqrtapproximation" class="header-anchor">#</a> SIMD.%FloatType%.reciprocalApproximation()，SIMD.%type%.reciprocalSqrtApproximation()</h3> <p><code>reciprocalApproximation</code>方法接受一个 SIMD 值作为参数，求出每个通道的倒数（<code>1 / x</code>），作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">reciprocalApproximation</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[1, 0.5, 0.3333333432674408, 0.25]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><code>reciprocalSqrtApproximation</code>方法接受一个 SIMD 值作为参数，求出每个通道的平方根的倒数（<code>1 / (x^0.5)</code>），作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">reciprocalSqrtApproximation</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">// Float32x4[1, 0.7071067690849304, 0.5773502588272095, 0.5]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意，只有浮点数的数据类型才有这两个方法。</p> <h3 id="simd-integertype-shiftleftbyscalar"><a href="#simd-integertype-shiftleftbyscalar" class="header-anchor">#</a> SIMD.%IntegerType%.shiftLeftByScalar()</h3> <p><code>shiftLeftByScalar</code>方法接受一个 SIMD 值作为参数，然后将每个通道的值左移指定的位数，作为一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">shiftLeftByScalar</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[2, 4, 8, 16]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果左移后，新的值超出了当前数据类型的位数，溢出的部分会被丢弃。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> ix4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> jx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">shiftLeftByScalar</span><span class="token punctuation">(</span>ix4<span class="token punctuation">,</span> <span class="token number">32</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[0, 0, 0, 0]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>注意，只有整数的数据类型才有这个方法。</p> <h3 id="simd-integertype-shiftrightbyscalar"><a href="#simd-integertype-shiftrightbyscalar" class="header-anchor">#</a> SIMD.%IntegerType%.shiftRightByScalar()</h3> <p><code>shiftRightByScalar</code>方法接受一个 SIMD 值作为参数，然后将每个通道的值右移指定的位数，返回一个新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">shiftRightByScalar</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[0, 1, 2, -4]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>如果原来通道的值是带符号的值，则符号位保持不变，不受右移影响。如果是不带符号位的值，则右移后头部会补<code>0</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Uint32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Uint32x4<span class="token punctuation">.</span><span class="token function">shiftRightByScalar</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Uint32x4[0, 1, 2, 2147483644]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中，<code>-8</code>右移一位变成了<code>2147483644</code>，是因为对于 32 位无符号整数来说，<code>-8</code>的二进制形式是<code>11111111111111111111111111111000</code>，右移一位就变成了<code>01111111111111111111111111111100</code>，相当于<code>2147483644</code>。</p> <p>注意，只有整数的数据类型才有这个方法。</p> <h2 id="静态方法-通道处理"><a href="#静态方法-通道处理" class="header-anchor">#</a> 静态方法：通道处理</h2> <h3 id="simd-type-check"><a href="#simd-type-check" class="header-anchor">#</a> SIMD.%type%.check()</h3> <p><code>check</code>方法用于检查一个值是否为当前类型的 SIMD 值。如果是的，就返回这个值，否则就报错。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">check</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[1, 2, 3, 9]</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 报错</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">check</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span> <span class="token comment">// 报错</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">check</span><span class="token punctuation">(</span><span class="token string">'hello world'</span><span class="token punctuation">)</span> <span class="token comment">// 报错</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="simd-type-extractlane-simd-type-replacelane"><a href="#simd-type-extractlane-simd-type-replacelane" class="header-anchor">#</a> SIMD.%type%.extractLane()，SIMD.%type%.replaceLane()</h3> <p><code>extractLane</code>方法用于返回给定通道的值。它接受两个参数，分别是 SIMD 值和通道编号。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token comment">// 3</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><code>replaceLane</code>方法用于替换指定通道的值，并返回一个新的 SIMD 值。它接受三个参数，分别是原来的 SIMD 值、通道编号和新的通道值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">replaceLane</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span>
<span class="token comment">// Float32x4[1, 2, 42, 4]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="simd-type-load"><a href="#simd-type-load" class="header-anchor">#</a> SIMD.%type%.load()</h3> <p><code>load</code>方法用于从二进制数组读入数据，生成一个新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[1, 2, 3, 4]</span>

<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[3, 4, 5, 6]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><code>load</code>方法接受两个参数：一个二进制数组和开始读取的位置（从 0 开始）。如果位置不合法（比如<code>-1</code>或者超出二进制数组的大小），就会抛出一个错误。</p> <p>这个方法还有三个变种<code>load1()</code>、<code>load2()</code>、<code>load3()</code>，表示从指定位置开始，只加载一个通道、二个通道、三个通道的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token comment">// 格式</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> index<span class="token punctuation">)</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load1</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> index<span class="token punctuation">)</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load2</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> index<span class="token punctuation">)</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load3</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> index<span class="token punctuation">)</span>

<span class="token comment">// 实例</span>
<span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load1</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[1, 0, 0, 0]</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load2</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[1, 2, 0, 0]</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">load3</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[1, 2, 3,0]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="simd-type-store"><a href="#simd-type-store" class="header-anchor">#</a> SIMD.%type%.store()</h3> <p><code>store</code>方法用于将一个 SIMD 值，写入一个二进制数组。它接受三个参数，分别是二进制数组、开始写入的数组位置、SIMD 值。它返回写入值以后的二进制数组。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> v1 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">store</span><span class="token punctuation">(</span>t1<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> v1<span class="token punctuation">)</span>
<span class="token comment">// Int32Array[1, 2, 3, 4, 0, 0, 0, 0]</span>

<span class="token keyword">var</span> t2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> v2 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">store</span><span class="token punctuation">(</span>t2<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> v2<span class="token punctuation">)</span>
<span class="token comment">// Int32Array[0, 0, 1, 2, 3, 4, 0, 0]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>上面代码中，<code>t1</code>是一个二进制数组，<code>v1</code>是一个 SIMD 值，只有四个通道。所以写入<code>t1</code>以后，只有前四个位置有值，后四个位置都是 0。而<code>t2</code>是从 2 号位置开始写入，所以前两个位置和后两个位置都是 0。</p> <p>这个方法还有三个变种<code>store1()</code>、<code>store2()</code>和<code>store3()</code>，表示只写入一个通道、二个通道和三个通道的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> tarray <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Int32Array</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> value <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">store1</span><span class="token punctuation">(</span>tarray<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32Array[1, 0, 0, 0, 0, 0, 0, 0]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="simd-type-splat"><a href="#simd-type-splat" class="header-anchor">#</a> SIMD.%type%.splat()</h3> <p><code>splat</code>方法返回一个新的 SIMD 值，该值的所有通道都会设成同一个预先给定的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">splat</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[3, 3, 3, 3]</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">splat</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float64x2[3, 3]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果省略参数，所有整数型的 SIMD 值都会设定<code>0</code>，浮点型的 SIMD 值都会设成<code>NaN</code>。</p> <h3 id="simd-type-swizzle"><a href="#simd-type-swizzle" class="header-anchor">#</a> SIMD.%type%.swizzle()</h3> <p><code>swizzle</code>方法返回一个新的 SIMD 值，重新排列原有的 SIMD 值的通道顺序。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">swizzle</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[2,3,1,4]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中，<code>swizzle</code>方法的第一个参数是原有的 SIMD 值，后面的参数对应将要返回的 SIMD 值的四个通道。它的意思是新的 SIMD 的四个通道，依次是原来 SIMD 值的 1 号通道、2 号通道、0 号通道、3 号通道。由于 SIMD 值最多可以有 16 个通道，所以<code>swizzle</code>方法除了第一个参数以外，最多还可以接受 16 个参数。</p> <p>下面是另一个例子。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[1.0, 2.0, 3.0, 4.0]</span>

<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">swizzle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[1.0, 1.0, 2.0, 2.0]</span>

<span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">swizzle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[4.0, 4.0, 4.0, 4.0]</span>

<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">swizzle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[4.0, 3.0, 2.0, 1.0]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="simd-type-shuffle"><a href="#simd-type-shuffle" class="header-anchor">#</a> SIMD.%type%.shuffle()</h3> <p><code>shuffle</code>方法从两个 SIMD 值之中取出指定通道，返回一个新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">shuffle</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[2, 6, 8, 3]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>上面代码中，<code>a</code>和<code>b</code>一共有 8 个通道，依次编号为 0 到 7。<code>shuffle</code>根据编号，取出相应的通道，返回一个新的 SIMD 值。</p> <h2 id="静态方法-比较运算"><a href="#静态方法-比较运算" class="header-anchor">#</a> 静态方法：比较运算</h2> <h3 id="simd-type-equal-simd-type-notequal"><a href="#simd-type-equal-simd-type-notequal" class="header-anchor">#</a> SIMD.%type%.equal()，SIMD.%type%.notEqual()</h3> <p><code>equal</code>方法用来比较两个 SIMD 值<code>a</code>和<code>b</code>的每一个通道，根据两者是否精确相等（<code>a === b</code>），得到一个布尔值。最后，所有通道的比较结果，组成一个新的 SIMD 值，作为掩码返回。<code>notEqual</code>方法则是比较两个通道是否不相等（<code>a !== b</code>）。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">equal</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span>
<span class="token comment">// Bool32x4[true, false, false, true]</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">notEqual</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Bool32x4[false, true, true, false]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="simd-type-greaterthan-simd-type-greaterthanorequal"><a href="#simd-type-greaterthan-simd-type-greaterthanorequal" class="header-anchor">#</a> SIMD.%type%.greaterThan()，SIMD.%type%.greaterThanOrEqual()</h3> <p><code>greatThan</code>方法用来比较两个 SIMD 值<code>a</code>和<code>b</code>的每一个通道，如果在该通道中，<code>a</code>较大就得到<code>true</code>，否则得到<code>false</code>。最后，所有通道的比较结果，组成一个新的 SIMD 值，作为掩码返回。<code>greaterThanOrEqual</code>则是比较<code>a</code>是否大于等于<code>b</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">greaterThan</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Bool32x4[false, true, false, true]</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">greaterThanOrEqual</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Bool32x4[true, true, false, true]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="simd-type-lessthan-simd-type-lessthanorequal"><a href="#simd-type-lessthan-simd-type-lessthanorequal" class="header-anchor">#</a> SIMD.%type%.lessThan()，SIMD.%type%.lessThanOrEqual()</h3> <p><code>lessThan</code>方法用来比较两个 SIMD 值<code>a</code>和<code>b</code>的每一个通道，如果在该通道中，<code>a</code>较小就得到<code>true</code>，否则得到<code>false</code>。最后，所有通道的比较结果，会组成一个新的 SIMD 值，作为掩码返回。<code>lessThanOrEqual</code>方法则是比较<code>a</code>是否等于<code>b</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">lessThan</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Bool32x4[false, true, true, false]</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">lessThanOrEqual</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Bool32x4[true, true, true, false]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="simd-type-select"><a href="#simd-type-select" class="header-anchor">#</a> SIMD.%type%.select()</h3> <p><code>select</code>方法通过掩码生成一个新的 SIMD 值。它接受三个参数，分别是掩码和两个 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> mask <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span>mask<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[1, 6, 7, 4]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>上面代码中，<code>select</code>方法接受掩码和两个 SIMD 值作为参数。当某个通道对应的掩码为<code>true</code>时，会选择第一个 SIMD 值的对应通道，否则选择第二个 SIMD 值的对应通道。</p> <p>这个方法通常与比较运算符结合使用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> mask <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">lessThan</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Bool32x4[false, false, true, true]</span>

<span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">select</span><span class="token punctuation">(</span>mask<span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[0, 6, 3, 4]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>上面代码中，先通过<code>lessThan</code>方法生成一个掩码，然后通过<code>select</code>方法生成一个由每个通道的较小值组成的新的 SIMD 值。</p> <h3 id="simd-booleantype-alltrue-simd-booleantype-anytrue"><a href="#simd-booleantype-alltrue-simd-booleantype-anytrue" class="header-anchor">#</a> SIMD.%BooleanType%.allTrue()，SIMD.%BooleanType%.anyTrue()</h3> <p><code>allTrue</code>方法接受一个 SIMD 值作为参数，然后返回一个布尔值，表示该 SIMD 值的所有通道是否都为<code>true</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Bool32x4<span class="token punctuation">.</span><span class="token function">allTrue</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Bool32x4<span class="token punctuation">.</span><span class="token function">allTrue</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><code>anyTrue</code>方法则是只要有一个通道为<code>true</code>，就返回<code>true</code>，否则返回<code>false</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Bool32x4</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Bool32x4<span class="token punctuation">.</span><span class="token function">anyTrue</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Bool32x4<span class="token punctuation">.</span><span class="token function">anyTrue</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>注意，只有四种布尔值数据类型（<code>Bool32x4</code>、<code>Bool16x8</code>、<code>Bool8x16</code>、<code>Bool64x2</code>）才有这两个方法。</p> <p>这两个方法通常与比较运算符结合使用。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> ax4    <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> bx4    <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">,</span> <span class="token number">6.0</span><span class="token punctuation">,</span> <span class="token number">7.0</span><span class="token punctuation">,</span> <span class="token number">8.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> ix4    <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">lessThan</span><span class="token punctuation">(</span>ax4<span class="token punctuation">,</span> bx4<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b1     <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">allTrue</span><span class="token punctuation">(</span>ix4<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// false</span>
<span class="token keyword">var</span> b2     <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">anyTrue</span><span class="token punctuation">(</span>ix4<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="simd-type-min-simd-type-minnum"><a href="#simd-type-min-simd-type-minnum" class="header-anchor">#</a> SIMD.%type%.min()，SIMD.%type%.minNum()</h3> <p><code>min</code>方法接受两个 SIMD 值作为参数，将两者的对应通道的较小值，组成一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5.2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">5.5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[-1, -4, 3, 5.2]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果有一个通道的值是<code>NaN</code>，则会优先返回<code>NaN</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">Infinity</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">1337</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float64x2[NaN, 42]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>minNum</code>方法与<code>min</code>的作用一模一样，唯一的区别是如果有一个通道的值是<code>NaN</code>，则会优先返回另一个通道的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> ax4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> bx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> cx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>ax4<span class="token punctuation">,</span> bx4<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[1.0, 1.0, NaN, NaN]</span>
<span class="token keyword">var</span> dx4 <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">minNum</span><span class="token punctuation">(</span>ax4<span class="token punctuation">,</span> bx4<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[1.0, 1.0, 3.0, NaN]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="simd-type-max-simd-type-maxnum"><a href="#simd-type-max-simd-type-maxnum" class="header-anchor">#</a> SIMD.%type%.max()，SIMD.%type%.maxNum()</h3> <p><code>max</code>方法接受两个 SIMD 值作为参数，将两者的对应通道的较大值，组成一个新的 SIMD 值返回。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5.2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">5.5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Float32x4[0, -2, 6, 5.5]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>如果有一个通道的值是<code>NaN</code>，则会优先返回<code>NaN</code>。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">Infinity</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">1337</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span>
<span class="token comment">// Float64x2[NaN, Infinity]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>maxNum</code>方法与<code>max</code>的作用一模一样，唯一的区别是如果有一个通道的值是<code>NaN</code>，则会优先返回另一个通道的值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> c <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">,</span> <span class="token number">Infinity</span><span class="token punctuation">)</span>
<span class="token keyword">var</span> d <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float64x2</span><span class="token punctuation">(</span><span class="token number">1337</span><span class="token punctuation">,</span> <span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Float64x2<span class="token punctuation">.</span><span class="token function">maxNum</span><span class="token punctuation">(</span>c<span class="token punctuation">,</span> d<span class="token punctuation">)</span>
<span class="token comment">// Float64x2[1337, Infinity]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="静态方法-位运算"><a href="#静态方法-位运算" class="header-anchor">#</a> 静态方法：位运算</h2> <h3 id="simd-type-and-simd-type-or-simd-type-xor-simd-type-not"><a href="#simd-type-and-simd-type-or-simd-type-xor-simd-type-not" class="header-anchor">#</a> SIMD.%type%.and()，SIMD.%type%.or()，SIMD.%type%.xor()，SIMD.%type%.not()</h3> <p><code>and</code>方法接受两个 SIMD 值作为参数，返回两者对应的通道进行二进制<code>AND</code>运算（<code>&amp;</code>）后得到的新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">and</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Int32x4[1, 0, 4, 0]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>上面代码中，以通道<code>0</code>为例，<code>1</code>的二进制形式是<code>0001</code>，<code>5</code>的二进制形式是<code>01001</code>，所以进行<code>AND</code>运算以后，得到<code>0001</code>。</p> <p><code>or</code>方法接受两个 SIMD 值作为参数，返回两者对应的通道进行二进制<code>OR</code>运算（<code>|</code>）后得到的新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">or</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Int32x4[5, 7, 5, 13]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>xor</code>方法接受两个 SIMD 值作为参数，返回两者对应的通道进行二进制“异或”运算（<code>^</code>）后得到的新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> b <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">xor</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span>
<span class="token comment">// Int32x4[4, 7, 1, 13]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><code>not</code>方法接受一个 SIMD 值作为参数，返回每个通道进行二进制“否”运算（<code>~</code>）后得到的新的 SIMD 值。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Int32x4</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">not</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span>
<span class="token comment">// Int32x4[-2, -3, -5, -9]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中，<code>1</code>的否运算之所以得到<code>-2</code>，是因为在计算机内部，负数采用”2 的补码“这种形式进行表示。也就是说，整数<code>n</code>的负数形式<code>-n</code>，是对每一个二进制位取反以后，再加上 1。因此，直接取反就相当于负数形式再减去 1，比如<code>1</code>的负数形式是<code>-1</code>，再减去 1，就得到了<code>-2</code>。</p> <h2 id="静态方法-数据类型转换"><a href="#静态方法-数据类型转换" class="header-anchor">#</a> 静态方法：数据类型转换</h2> <p>SIMD 提供以下方法，用来将一种数据类型转为另一种数据类型。</p> <ul><li><code>SIMD.%type%.fromFloat32x4()</code></li> <li><code>SIMD.%type%.fromFloat32x4Bits()</code></li> <li><code>SIMD.%type%.fromFloat64x2Bits()</code></li> <li><code>SIMD.%type%.fromInt32x4()</code></li> <li><code>SIMD.%type%.fromInt32x4Bits()</code></li> <li><code>SIMD.%type%.fromInt16x8Bits()</code></li> <li><code>SIMD.%type%.fromInt8x16Bits()</code></li> <li><code>SIMD.%type%.fromUint32x4()</code></li> <li><code>SIMD.%type%.fromUint32x4Bits()</code></li> <li><code>SIMD.%type%.fromUint16x8Bits()</code></li> <li><code>SIMD.%type%.fromUint8x16Bits()</code></li></ul> <p>带有<code>Bits</code>后缀的方法，会原封不动地将二进制位拷贝到新的数据类型；不带后缀的方法，则会进行数据类型转换。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">fromFloat32x4</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[1, 2, 3, 4]</span>

<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int32x4<span class="token punctuation">.</span><span class="token function">fromFloat32x4Bits</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int32x4[1065353216, 1073741824, 1077936128, 1082130432]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>上面代码中，<code>fromFloat32x4</code>是将浮点数转为整数，然后存入新的数据类型；<code>fromFloat32x4Bits</code>则是将二进制位原封不动地拷贝进入新的数据类型，然后进行解读。</p> <p><code>Bits</code>后缀的方法，还可以用于通道数目不对等的拷贝。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> t <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">1.0</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">,</span> <span class="token number">3.0</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token constant">SIMD</span><span class="token punctuation">.</span>Int16x8<span class="token punctuation">.</span><span class="token function">fromFloat32x4Bits</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Int16x8[0, 16256, 0, 16384, 0, 16448, 0, 16512]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>上面代码中，原始 SIMD 值<code>t</code>是 4 通道的，而目标值是 8 通道的。</p> <p>如果数据转换时，原通道的数据大小，超过了目标通道的最大宽度，就会报错。</p> <h2 id="实例方法"><a href="#实例方法" class="header-anchor">#</a> 实例方法</h2> <h3 id="simd-type-prototype-tostring"><a href="#simd-type-prototype-tostring" class="header-anchor">#</a> SIMD.%type%.prototype.toString()</h3> <p><code>toString</code>方法返回一个 SIMD 值的字符串形式。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">var</span> a <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span><span class="token function">Float32x4</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
a<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// &quot;SIMD.Float32x4(11, 22, 33, 44)&quot;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="实例-求平均值"><a href="#实例-求平均值" class="header-anchor">#</a> 实例：求平均值</h2> <p>正常模式下，计算<code>n</code>个值的平均值，需要运算<code>n</code>次。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">average</span><span class="token punctuation">(</span><span class="token parameter">list</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> n <span class="token operator">=</span> list<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
  <span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token number">0.0</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    sum <span class="token operator">+=</span> list<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">return</span> sum <span class="token operator">/</span> n<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>使用 SIMD，可以将计算次数减少到<code>n</code>次的四分之一。</p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">function</span> <span class="token function">average</span><span class="token punctuation">(</span><span class="token parameter">list</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> n <span class="token operator">=</span> list<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
  <span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">splat</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> n<span class="token punctuation">;</span> i <span class="token operator">+=</span> <span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    sum <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>
      sum<span class="token punctuation">,</span>
      <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>list<span class="token punctuation">,</span> i<span class="token punctuation">)</span>
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">var</span> total <span class="token operator">=</span> <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>sum<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token operator">+</span>
              <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>sum<span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">+</span>
              <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>sum<span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token operator">+</span>
              <span class="token constant">SIMD</span><span class="token punctuation">.</span>Float32x4<span class="token punctuation">.</span><span class="token function">extractLane</span><span class="token punctuation">(</span>sum<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> total <span class="token operator">/</span> n<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>上面代码先是每隔四位，将所有的值读入一个 SIMD，然后立刻累加。然后，得到累加值四个通道的总和，再除以<code>n</code>就可以了。</p></div></div> <div class="page-slot page-slot-bottom"><!-- 横向自适应 -->
      <ins class="adsbygoogle"
          style="display:block"
          data-ad-client="ca-pub-7828333725993554"
          data-ad-slot="6620245489"
          data-ad-format="auto"
          data-full-width-responsive="true"></ins>
      <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
      </script></div> <div class="page-edit"><div class="edit-link"><a href="https://github.com/linbingxing/vuepress-theme-vdoing/edit/master/docs/《ES6 教程》笔记/33.SIMD.md" target="_blank" rel="noopener noreferrer">编辑</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="tags"><a href="/tags/?tag=ES6" title="标签">#ES6</a></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021/11/29, 10:58:58</span></div></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/6a8e2dc558da1b39/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">Mixin</div></a> <a href="/pages/ea6f3b870f6dab69/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">参考链接</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/6a8e2dc558da1b39/" class="prev">Mixin</a></span> <span class="next"><a href="/pages/ea6f3b870f6dab69/">参考链接</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/e7004a/"><div>WeakHashMap源码解析</div></a> <span>11-28</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/e4a61a/"><div>TreeSet &amp; TreeMap 源码解析</div></a> <span>11-28</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/7bd9aa/"><div>LinkedHashSet&amp;Map源码解析</div></a> <span>11-28</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:linbingxing@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/linbingxing" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2020-2021
    <span>林半码 | <a href="https://gitee.com/linbingxing/vuepress_doc/blob/master/LICENSE" target="_blank">MIT License</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <div class="custom-html-window custom-html-window-rb" style="display:;"><div class="custom-wrapper"><i class="close-but">×</i> <div><!-- 固定160*160px -->
      <ins class="adsbygoogle"
          style="display:inline-block;max-width:160px;max-height:160px"
          data-ad-client="ca-pub-7828333725993554"
          data-ad-slot="8377369658"></ins>
      <script>
          (adsbygoogle = window.adsbygoogle || []).push({});
      </script>
      </div></div></div></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.66ec18f0.js" defer></script><script src="/assets/js/2.94569d7c.js" defer></script><script src="/assets/js/132.7c898663.js" defer></script>
  </body>
</html>
